package com.my.lock.v24;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Scarlett on 2018/9/13 0013.
 */
public class NonFairLock {
    private static final Logger LOGGER = LoggerFactory.getLogger(NonFairLock.class);

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        ReentrantLock lock = new ReentrantLock(false);

        for (int i = 0; i < 100; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        lock.lock();
                        LOGGER.info("locked by {}", Thread.currentThread().getName());
                    } finally {
                        lock.unlock();
                    }
                }

            });
        }
    }
}
